package com.example.apidemo2;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Spinner;  
import android.widget.AdapterView;  
import android.widget.ArrayAdapter;  
import android.widget.SimpleAdapter;

import com.example.apidemo2.R;
import com.pos.api.Pci;
import com.pos.utils.ByteUtil;
import com.pos.utils.StringUtil;


public class PciActivity extends Activity implements OnClickListener {  
    /** Called when the activity is first created. */  
	private Pci pci;

	private Spinner spinnerType; 
	private Spinner spinnerIndex;
	private TextView textRecord;

	
	private Button btnKeyWrite;
	private Button btnPinEnc;
	private Button btnMacEnc;
	private Button btnDesEnc;
	private Button btnFixPin;
	private Button btnDukptPin;
	private Button btnDukptMac;
	private Button btnDukptDes;
	private Button btnGetRnd;
	private Button btnGetPinExit;
	private Button btnKbOpen,btnKbClose,btnKbGetKey;
	private static final int RECORD_PROMPT_MSG = 0x01;
	
	private Handler promptHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RECORD_PROMPT_MSG:
                	showTextRecordMsg(msg);
                    break;
                default:
                	break;
            }

        }
    };
    
	private void showTextRecordMsg( Message msg)
	{
		Bundle b = msg.getData();
        String strInfo = b.getString("MSG");
        if(strInfo.equals("")) textRecord.setText("");
        else textRecord.append(strInfo);
	       
	}
	
    public void SendPromptMsg(String strInfo){
        Message msg = new Message();
        msg.what = RECORD_PROMPT_MSG;
        Bundle b = new Bundle();
        b.putString("MSG", strInfo);
        msg.setData(b);
        promptHandler.sendMessage(msg);
    }
		
	
  @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_pci); 		
                  
		pci = new Pci(this, new Pci.PciConnectStatusListener(){
			@Override
			public void onConnectResult(boolean bRet) {	
			}
		});
		
		btnKeyWrite = (Button) findViewById(R.id.btnKeyWrite);
		btnKeyWrite.setOnClickListener(this);
		btnPinEnc = (Button) findViewById(R.id.btnPinEnc);
		btnPinEnc.setOnClickListener(this);
		btnMacEnc = (Button) findViewById(R.id.btnMacEnc);
		btnMacEnc.setOnClickListener(this);
		btnDesEnc = (Button) findViewById(R.id.btnDesEnc);
		btnDesEnc.setOnClickListener(this);
		btnFixPin = (Button) findViewById(R.id.btnFixPin);
		btnFixPin.setOnClickListener(this);
		btnDukptPin = (Button) findViewById(R.id.btnDukptPin);
		btnDukptPin.setOnClickListener(this);
		btnDukptMac = (Button) findViewById(R.id.btnDukptMac);
		btnDukptMac.setOnClickListener(this);
		btnDukptDes = (Button) findViewById(R.id.btnDukptDes);
		btnDukptDes.setOnClickListener(this);
		btnGetRnd = (Button) findViewById(R.id.btnGetRnd);
		btnGetRnd.setOnClickListener(this);
		btnGetPinExit = (Button) findViewById(R.id.btnGetPinExit);
		btnGetPinExit.setOnClickListener(this);
		btnKbOpen = (Button) findViewById(R.id.btnKbOpen);
		btnKbOpen.setOnClickListener(this);
		btnKbGetKey = (Button) findViewById(R.id.btnKbGetKey);
		btnKbGetKey.setOnClickListener(this);
		btnKbClose = (Button) findViewById(R.id.btnKbClose);
		btnKbClose.setOnClickListener(this);
		
		textRecord=(TextView)this.findViewById(R.id.textRecord);
		
		spinnerType=(Spinner) findViewById(R.id.spinnerType);  

		final String type[]=new String[]{  
		"TMK",  
		"FIXK",
		"SK_MACK",
		"SM4MK",
		"SM4_SK_MACK",
		"PINK",  
		"MACK",
		"DESK",
		"SM4_PINK",  
		"SM4_MACK",
		"SM4_DESK",
		"DUKPT",
		"KEYBOARD"
		};  
		
		ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, type);  
		typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerType.setAdapter(typeAdapter);  



		spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
		
			@Override  
			public void onItemSelected(AdapterView<?> parent, View view,  
					int position, long id) {  
				Spinner spinner=(Spinner) parent;  
			}  
			
			@Override  
			public void onNothingSelected(AdapterView<?> parent) {
				Toast.makeText(getApplicationContext(),"û�иı�Ĵ���",Toast.LENGTH_LONG).show();
			}  		
		});  
//		

		spinnerIndex=(Spinner) findViewById(R.id.SpinnerIndex);  

		final String index[]=new String[]{  
		"0",  
		"1",
		"2",
		"3",
		"4",
		"5"
		};  
		
		ArrayAdapter<String> indexAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, index);  
		indexAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerIndex.setAdapter(indexAdapter);  

		spinnerIndex.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
		
			@Override  
			public void onItemSelected(AdapterView<?> parent, View view,  
					int position, long id) {  
				Spinner spinner=(Spinner) parent;  
			}  
			
			@Override  
			public void onNothingSelected(AdapterView<?> parent) {
				Toast.makeText(getApplicationContext(),"û�иı�Ĵ���",Toast.LENGTH_LONG).show();
			}  		
		}); 	
	}  

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	
		byte [] background = new byte[3];
		byte [] font = new byte [4];
		byte [] prompt = "Please Cover The Keypad With Hand And Enter PIN:".getBytes();
		com.pos.utils.PinContext ctx = new com.pos.utils.PinContext();

		background[0] = (byte)0x00;//R
		background[1] = (byte)0x00;//G
		background[2] = (byte)0x00;//B
		font[0] = (byte)0xff;//R
		font[1] = (byte)0x00;//G
		font[2] = (byte)0xff;//B
		font[3] = 20;//text size(dp)
		ctx.setPromptLength(prompt.length);
		ctx.setFontLength(font.length);
		ctx.setBackgroundLength(background.length);					
		ctx.setPrompt(prompt);   
		ctx.setFont(font);
		ctx.setBackground(background);
		
		switch(v.getId())
		{
		case R.id.btnKeyWrite:
			//new Thread() {
			//	public void run() {
					int iRet = -1;
					byte type = 0;
					byte index = 0;
					byte mode = 0;
					byte [] keyData = new byte[24];
					byte [] ksn = new byte [10];	
					byte [] checkValue = new byte[16];
					SendPromptMsg("");
					type = (byte)(spinnerType.getSelectedItemPosition());
					index = (byte)(spinnerIndex.getSelectedItemPosition());
					if(type >= 0 && type <= 4)
					{
						if(type == 0) keyData = StringUtil.hexStringToBytes("0123456789ABCDEF0123456789ABCDEF");
						if(type == 1) keyData = StringUtil.hexStringToBytes("22222222222222222222222222222222");
						if(type == 2) keyData = StringUtil.hexStringToBytes("11111111111111111111111111111111");
						if(type == 3) keyData = StringUtil.hexStringToBytes("2AD61E3F44DBD914E0C837058F0402FD");
						if(type == 4) keyData = StringUtil.hexStringToBytes("22222222222222224444444444444444");
						mode = 0;
						if(type==0||type==3)
							iRet = pci.DLL_PciWriteMKey(type, index, (byte)16, keyData, mode,checkValue);
						else iRet = pci.DLL_PciWriteMKey(type, index, (byte)16, keyData, mode);
						if(iRet == 0) {
							if(type==0)
								SendPromptMsg("Write master key success!!!  KeyValue:"+ByteUtil.bytearrayToHexString(checkValue, 8)+"\r\n");
							else if(type==3) SendPromptMsg("Write master key success!!!  KeyValue:"+ByteUtil.bytearrayToHexString(checkValue, 16)+"\r\n");
							else SendPromptMsg("Write master key success!!!"+"\r\n");
						}
						else {SendPromptMsg("Write master key failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					}
					else if(type >= 5 && type <= 10)
					{
						if(type == 5) keyData = StringUtil.hexStringToBytes("22E448E26301E0942AEB75DD234EC11A");//97851597BF5AD443 97851597BF5AD443
						if(type == 6) keyData = StringUtil.hexStringToBytes("66666666666666666666666666666666");//9AFB86CFB4016760 9AFB86CFB4016760
						if(type == 7) keyData = StringUtil.hexStringToBytes("55555555555555557777777777777777");//B8AB5D29916A6401 B8AB5D29916A6401
						if(type == 8) keyData = StringUtil.hexStringToBytes("FF643D84B2B4D8FD975F8FA20E6B5B0F");
						if(type == 9) keyData = StringUtil.hexStringToBytes("66666666666666667777777777777777");
						if(type == 10) keyData = StringUtil.hexStringToBytes("77777777777777776666666666666666");
						mode = (byte)0x81;
						iRet = pci.DLL_PciWriteSKey((byte)(type), index, (byte)16, keyData, mode, index,checkValue);
						if(iRet == 0) {
							if(type>=5&&type<=7)
								SendPromptMsg("Write session key success!!!  KeyValue:"+ByteUtil.bytearrayToHexString(checkValue, 8)+"\r\n");
							else SendPromptMsg("Write session key success!!!  KeyValue:"+ByteUtil.bytearrayToHexString(checkValue, 16)+"\r\n");
							
						}
						else {SendPromptMsg("Write session key failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					}
					else if(type == 12){
						keyData = new byte[26];
						
					//	static  unsigned char Key_Tab[26]={
					//			KEYCANCEL,   KEY3,       KEY2,      KEY1, 
					//			KEYUP,   KEY6,       KEY5,      KEY4,
					//			KEYDOWN,  KEY9,       KEY8,      KEY7,
					//			KEYENTER,   KEYBACKSPACE,         KEY0,      F1,      
					//			0,         0,       0,         0,  
					//			0,         0,       0,         0,
					//			0,         0 
					//			};

						keyData[0] = 0x1b;//KEYCANCEL
						keyData[1] = 0x33;//KEY3
						keyData[2] = 0x32;//KEY2
						keyData[3] = 0x31;//KEY1
						keyData[4] = 0x05;//KEYUP
						keyData[5] = 0x36;//KEY6
						keyData[6] = 0x35;//KEY5
						keyData[7] = 0x34;//KEY4
						keyData[8] = 0x06;//KEYDOWN
						keyData[9] = 0x39;//KEY9
						keyData[10] = 0x38;//KEY8
						keyData[11] = 0x37;//KEY7
						keyData[12] = 0x0d;//KEYENTER
						keyData[13] = 0x1c;//KEYBACKSPACE
						keyData[14] = 0x30;//KEY0
						keyData[15] = 0x1e;//F1
						keyData[16] = 0x00;
						keyData[17] = 0x00;
						keyData[18] = 0x00;
						keyData[19] = 0x00;
						keyData[20] = 0x00;
						keyData[21] = 0x00;
						keyData[22] = 0x00;
						keyData[23] = 0x00;
						keyData[24] = 0x00;
						keyData[25] = 0x00;

						iRet = pci.DLL_PciKbLoadKeyboard(keyData);
						if(iRet == 0) {
							SendPromptMsg("Write keyboard keys success!!!");
						}
						else {SendPromptMsg("Write keyboard keys failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					}
					else
					{                                          
						keyData = StringUtil.hexStringToBytes("6AC292FAA1315B4D858AB3A3D7D5933A");
						ksn = StringUtil.hexStringToBytes("FFFF9876543210E00000");
						mode = 0;
						iRet = pci.DLL_PciWriteDukptKey((byte)(type), index, (byte)16, keyData, (byte)10, ksn, mode);
						if(iRet == 0) SendPromptMsg("Write dukpt key success!!!"+"\r\n");
						else {SendPromptMsg("Write dukpt key failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					}	
			//	}
			//}.start();
			break;
		case R.id.btnPinEnc:
			new Thread() {
				public void run() {
					int iRet = -1;
					byte	index = (byte)(spinnerIndex.getSelectedItemPosition());
					byte [] cardNo = new byte[50];
					byte [] pinBlock = new byte[16];
					byte [] background = new byte[3];
					byte [] font = new byte [4];
					byte [] prompt = "Please Cover The Keypad With Hand And Enter PIN:".getBytes();
					com.pos.utils.PinContext ctx = new com.pos.utils.PinContext();

					SendPromptMsg("");
					cardNo = "6223200135299844".getBytes();
					
					background[0] = (byte)0xff;//R
					background[1] = (byte)0xff;//G
					background[2] = (byte)0xff;//B
					font[0] = (byte)0xff;//R
					font[1] = (byte)0x00;//G
					font[2] = (byte)0xff;//B
					font[3] = 20;//text size(dp)
					ctx.setPromptLength(prompt.length);
					ctx.setFontLength(font.length);
					ctx.setBackgroundLength(background.length);		
					ctx.setPrompt(prompt);   
					ctx.setFont(font);
					ctx.setBackground(background);		
					// first,mode |= 0x10
					
					iRet = pci.DLL_PciGetPin(index, (byte)4, (byte)12, (byte)16, cardNo, (byte)0, pinBlock, (byte)1, "123456.00".getBytes(), (byte)0, ctx);
					
					
					//iRet = pci.DLL_PciGetPin((byte)0, (byte)4, (byte)6, (byte)0, cardNo, (byte)0x10/*0x20 | 0x10*/, pinBlock,(byte)1, "123456.00".getBytes(), (byte)0, ctx);
					if(iRet == 0) {
						// second,mode |= 0x08
						//iRet = pci.DLL_PciGetPin(index, (byte)0, (byte)0, (byte)16, cardNo, (byte)0x08, pinBlock,(byte)0, null, (byte)0, ctx);
						
						SendPromptMsg("Get PINBLOCK by pink success!!!"+"\r\n"+"PINBLOCK is: "+ ByteUtil.bytearrayToHexString(pinBlock,8));
					}
					else {SendPromptMsg("Get PINBLOCK by pink failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
				}
			}.start();
			break;
		case R.id.btnFixPin:
			//new Thread() {
			//	public void run() {
					 iRet = -1;
						index = (byte)(spinnerIndex.getSelectedItemPosition());
					byte [] cardNo1 = new byte[50];
					byte [] pinBlock1 = new byte[16];
					

					SendPromptMsg("");
					cardNo1 = "1234567890123456".getBytes();
					
					background[0] = (byte)0xff;//R
					background[1] = (byte)0xff;//G
					background[2] = (byte)0xff;//B
					font[0] = (byte)0xff;//R
					font[1] = (byte)0x00;//G
					font[2] = (byte)0xff;//B
					font[3] = 20;//text size(dp)
					ctx.setPromptLength(prompt.length);
					ctx.setFontLength(font.length);
					ctx.setBackgroundLength(background.length);					
					ctx.setPrompt(prompt);   
					ctx.setFont(font);
					ctx.setBackground(background);
					
					iRet = pci.DLL_PciGetPinByFixKey(index, (byte)4, (byte)12, (byte)16, cardNo1, (byte)0, pinBlock1, (byte)1, "123456.00".getBytes(), (byte)0, ctx);
					if(iRet == 0) SendPromptMsg("Get PINBLOCK by fix key success!!!"+"\r\n"+"PINBLOCK is: "+ ByteUtil.bytearrayToHexString(pinBlock1,8));
					else {SendPromptMsg("Get PINBLOCK by fix key failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
			//	}
			//}.start();
			break;
		case R.id.btnDukptPin:
			//new Thread() {
			//	public void run() {
					 iRet = -1;
						index = (byte)(spinnerIndex.getSelectedItemPosition());
					byte [] cardNo11 = new byte[50];
					byte [] pinBlock11 = new byte[8];
					byte [] outKsn = new byte[10];
					
					
					SendPromptMsg("");
					cardNo11 = "4012345678909".getBytes();
					background[0] = (byte)0xff;//R
					background[1] = (byte)0xff;//G
					background[2] = (byte)0xff;//B
					font[0] = (byte)0xff;//R
					font[1] = (byte)0x00;//G
					font[2] = (byte)0xff;//B
					font[3] = 20;//text size(dp)
					ctx.setPromptLength(prompt.length);
					ctx.setFontLength(font.length);
					ctx.setBackgroundLength(background.length);					
					ctx.setPrompt(prompt);   
					ctx.setFont(font);
					ctx.setBackground(background);
					iRet = pci.DLL_PciGetPinDukpt(index, (byte)4, (byte)12, (byte)13, cardNo11, (byte)0, pinBlock11, (byte)1, "123456.00".getBytes(), (byte)0, outKsn, ctx);
					if(iRet == 0) SendPromptMsg("Get PINBLOCK by dukpt key success!!!"+"\r\n"+"PINBLOCK is: "+ ByteUtil.bytearrayToHexString(pinBlock11,8));
					else {SendPromptMsg("Get PINBLOCK by dukpt key failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
			//	}
			//}.start();
			break;
		case R.id.btnMacEnc:
			//new Thread() {
			//	public void run() {
					//int iRet = -1;
					index = (byte)(spinnerIndex.getSelectedItemPosition());
					byte [] datain = new byte[1024];
					byte [] dataout = new byte[16];
					//datain = StringUtil.hexStringToBytes("55555555555555555555555555555555");
					SendPromptMsg("");
					iRet = pci.DLL_PciGetMac(index, 1024, datain, dataout, (byte)0);
					if(iRet == 0) SendPromptMsg("MAC encryption mode 0 success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout,8));
					else {SendPromptMsg("MAC encryption mode 0 failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					iRet = pci.DLL_PciGetMac(index, 1024, datain, dataout, (byte)1);
					if(iRet == 0) SendPromptMsg("MAC encryption mode 1 success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout,8));
					else {SendPromptMsg("MAC encryption mode 1 failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					iRet = pci.DLL_PciGetMac(index, 1024, datain, dataout, (byte)2);
					if(iRet == 0) SendPromptMsg("MAC encryption mode 2 success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout,8));
					else {SendPromptMsg("MAC encryption mode 2 failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					iRet = pci.DLL_PciGetMac(index, 1024, datain, dataout, (byte)3);
					if(iRet == 0) SendPromptMsg("MAC encryption mode 3 success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout,8));
					else {SendPromptMsg("MAC encryption mode 3 failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
			//	}
			//}.start();
			break;
		case R.id.btnDesEnc:
			//new Thread() {
			//	public void run() {
					/*int iRet = -1;
					byte*/
					byte [] datain1 = new byte[1024];
					byte [] dataout1 = new byte[1024];
					index = (byte)(spinnerIndex.getSelectedItemPosition());
					//datain = StringUtil.hexStringToBytes("55555555555555555555555555555555");

					SendPromptMsg("");
					//iRet = pci.DLL_PciGetDes(index, 16, datain, dataout1, (byte)1);
					iRet = pci.DLL_PciGetDes(index, 1024, datain1, dataout1, (byte)0x81); // bit 7 means CBC
					if(iRet == 0) SendPromptMsg("DES encryption success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout1,16));
					else {SendPromptMsg("DES encryption failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
			//	}
			//}.start();
			break;
		case R.id.btnDukptMac:
			//new Thread() {
			//	public void run() {
					/*int iRet = -1;
					byte index = 1;
					byte [] datain = new byte[1024];*/
					byte [] dataout11 = new byte[16];
					byte [] outKsn1 = new byte[10];
					index = (byte)(spinnerIndex.getSelectedItemPosition());
					datain1 = "4012345678909D987".getBytes();
					SendPromptMsg("");
					iRet = pci.DLL_PciGetMacDukpt(index, 17, datain1, dataout11, (byte)2,outKsn1);
					if(iRet == 0) SendPromptMsg("MAC encryption mode 0 success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout11,8));
					else {SendPromptMsg("MAC encryption mode 0 failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					iRet = pci.DLL_PciGetMacDukpt(index, 17, datain1, dataout11, (byte)1,outKsn1);
					if(iRet == 0) SendPromptMsg("MAC encryption mode 1 success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout11,8));
					else {SendPromptMsg("MAC encryption mode 1 failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					iRet = pci.DLL_PciGetMacDukpt(index, 17, datain1, dataout11, (byte)0,outKsn1);
					if(iRet == 0) SendPromptMsg("MAC encryption mode 2 success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout11,8));
					else {SendPromptMsg("MAC encryption mode 2 failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
					iRet = pci.DLL_PciGetMacDukpt(index, 17, datain1, dataout11, (byte)3,outKsn1);
					if(iRet == 0) SendPromptMsg("MAC encryption mode 3 success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout11,8));
					else {SendPromptMsg("MAC encryption mode 3 failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
			//	}
			//}.start();
			break;
		case R.id.btnDukptDes:
			//new Thread() {
			//	public void run() {
					/*int iRet = -1;
					byte index = 1;
					byte [] datain = new byte[1024];*/
					byte [] dataout111 = new byte[1024];
					byte [] outKsn11 = new byte[10];
					index = (byte)(spinnerIndex.getSelectedItemPosition());
					 mode = 1;
					datain1 = "4012345678909D987".getBytes();
//3430313233343536.3738393039443938.3700000000000000
					SendPromptMsg("");
					iRet = pci.DLL_PciGetDesDukpt(index, 17, datain1, dataout111, mode,outKsn11);
					if(iRet == 0) SendPromptMsg("DES encryption success!!!"+"\r\n"+"dataout is: "+ ByteUtil.bytearrayToHexString(dataout111,24));
					else {SendPromptMsg("DES encryption failed, returen value is:"+Integer.toString(iRet)+"\r\n"); return;}
			//	}
			//}.start();
			break;
		case R.id.btnGetRnd:
			byte [] rnd = new byte [8];
			type = (byte)(spinnerType.getSelectedItemPosition());
			//if(type==8)
			{
				SendPromptMsg("");
				pci.DLL_PciGetRnd(rnd);
				SendPromptMsg("Get random success!!!"+"\r\n"+"random is: "+ ByteUtil.bytearrayToHexString(rnd,8));
			}
			break;
		case R.id.btnGetPinExit:
			pci.DLL_PciGetPinExit();
			break;
		case R.id.btnKbOpen:
			pci.DLL_PciKbOpen();
			break;
		case R.id.btnKbClose:
			pci.DLL_PciKbClose();
			break;
		case R.id.btnKbGetKey:
			byte[] keyVlaue = new byte[1];
			SendPromptMsg("");
			iRet = pci.DLL_PciKbGetKey(keyVlaue);
			if(iRet==0){
				SendPromptMsg("Get Kb Value success!!!"+" Value is: "+ ByteUtil.bytearrayToHexString(keyVlaue,1));
			}
			break;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(null!=pci) pci.DLL_PciRelease();
		super.onDestroy();
	}  
}

